using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class KhayDungDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveKhayDung(KhayDung khaydung)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddKhayDung", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddKhayDung";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, khaydung.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, khaydung.MaSo);
            cmd.Parameters.Add(param);
			param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, khaydung.Ten);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuongHop", DbType.Int64, ParameterDirection.Input, khaydung.SoLuongHop);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuongTrung", DbType.Int64, ParameterDirection.Input, khaydung.SoLuongTrung);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all KhayDung in DB
		public ResultObj GetAllKhayDung( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KhayDung> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKhayDung", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KhayDung>();
			KhayDung item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KhayDung();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.SoLuongHop = Convert.ToInt64(reader["SoLuongHop"]);
			item.SoLuongTrung = Convert.ToInt64(reader["SoLuongTrung"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all KhayDung in DB with condition
		public ResultObj GetAllKhayDungByID(KhayDung khaydung)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KhayDung> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKhayDungByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, khaydung.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KhayDung>();
			KhayDung item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KhayDung();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.SoLuongHop = Convert.ToInt64(reader["SoLuongHop"]);
			item.SoLuongTrung = Convert.ToInt64(reader["SoLuongTrung"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a KhayDung out of DB with condition
		public ResultObj DeleteKhayDung(KhayDung khaydung)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteKhayDung", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteKhayDung";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, khaydung.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of HangHoa from DB with condition
		public ResultObj GetHangHoa(int IdHangHoa)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<HangHoa> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetHangHoaByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdHangHoa);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<HangHoa>();
			HangHoa item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new HangHoa();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ChuoiTimKiem = Convert.ToString(reader["ChuoiTimKiem"]);
			item.NoiLuuTru = Convert.ToString(reader["NoiLuuTru"]);
			item.IdKhayDung = Convert.ToInt32(reader["IdKhayDung"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
